package util.common;

import exception.MibException;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class CharTypeChecker {
    private static final String METHOD_PREFIX = "is";

    /**
     * 通过反射检查字符是否符合类型
     * @param c
     * @param types
     * @return
     */
    public static boolean check(char c,CharType ... types) {
        try {
            Class clazz = CharTypeChecker.class;
            for (CharType t : types){
                String name = METHOD_PREFIX + String.valueOf(t);
                Method m = clazz.getDeclaredMethod(name, char.class);
                if ((Boolean) m.invoke(clazz, c)){
                    return true;
                }
            }
            return false;
        } catch (NoSuchMethodException e) {
            throw new MibException("无法检测当前类型语法。");
        } catch (IllegalAccessException e) {
            throw new MibException("无法检测当前类型语法。");
        } catch (InvocationTargetException e) {
            throw new MibException("无法检测当前类型语法。");
        }
    }

    public static boolean isLetter(char c)
    {
        return checkInScope(c, 'a', 'z', 'A', 'Z');
    }
    public static boolean isCapitalLetter(char c) {
        return checkInScope(c, 'A', 'Z');
    }
    public static boolean isSmallLetter(char c) {
        return checkInScope(c, 'a', 'z');
    }
    public static boolean isNumber(char c)
    {
        return checkInScope(c, '0', '9');
    }
    public static boolean isSpace(char c) {
        return checkIsEqual(c, ' ');
    }
    public static boolean isDqm(char c) {
        return checkIsEqual(c, '"');
    }
    public static boolean isLBrace(char c) {
        return checkIsEqual(c, '{');
    }
    public static boolean isRBrace(char c) {
        return checkIsEqual(c, '}');
    }
    public static boolean isEqual(char c) {
        return checkIsEqual(c, '=');
    }
    public static boolean isColon(char c) {
        return checkIsEqual(c, ':');
    }
    public static boolean isLParenthese(char c) {
        return checkIsEqual(c, '(');
    }
    public static boolean isRParenthese(char c) {
        return checkIsEqual(c, ')');
    }
    public static boolean isHyphen(char c) {
        return checkIsEqual(c, '-');
    }
    public static boolean isOr(char c) {
        return checkIsEqual(c, '|');
    }
    public static boolean isSemicolon(char c) {
        return checkIsEqual(c, ';');
    }
    public static boolean isLSquareBracket(char c) {
        return checkIsEqual(c, '[');
    }
    public static boolean isRSquareBracket(char c) {
        return checkIsEqual(c, ']');
    }
    public static boolean isComma(char c) {
        return checkIsEqual(c, ',');
    }

    /**
     * 判断字符c是否在chars的范围内。
     * @param c
     * @param chars
     * @return
     */
    private static boolean checkInScope(char c, char ... chars){
        if ((chars.length & 1) == 1)
        {
            throw new MibException("范围值必须是复数。");
        }

        boolean r = false;
        for (int i = 0; i < chars.length - 1; i += 2)
        {
            if (c >= chars[i] && c <= chars[i + 1])
            {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断字符c是否与chars某个值相同。
     * @param c
     * @return
     */
    private static boolean checkIsEqual(char c, char ... chars){
        for (int i = 0; i < chars.length; i++)
        {
            if (c == chars[i])
            {
                return true;
            }
        }
        return false;
    }
}
